package cn.cleanarch.infra.idp.pms.utils;

public class SecurePreviewTokenUtilDemo {
    public static void main(String[] args) {
        System.out.println("=== 测试新安全令牌机制 ===");
        
        // 创建实例并设置密钥
        SecurePreviewTokenUtil tokenUtil = new SecurePreviewTokenUtil();
        
        try {
            // 使用反射设置私有字段
            java.lang.reflect.Field secretKeyField = SecurePreviewTokenUtil.class.getDeclaredField("secretKey");
            secretKeyField.setAccessible(true);
            secretKeyField.set(tokenUtil, "testSecretKey12345");
            
            System.out.println("1. 测试双重MD5功能:");
            String input = "testString";
            String result = tokenUtil.doubleMd5(input);
            System.out.println("   输入: " + input);
            System.out.println("   双重MD5结果: " + result);
            System.out.println("   包含+号: " + result.contains("+"));
            
            System.out.println("\n2. 测试签名生成和验证:");
            Long fileId = 123L;
            SecurePreviewTokenUtil.SignParams signParams = tokenUtil.generateSignParams(fileId, 3600000L); // 1小时
            System.out.println("   文件ID: " + signParams.fileId);
            System.out.println("   时间戳: " + signParams.timestamp);
            System.out.println("   Nonce: " + signParams.nonce);
            System.out.println("   签名: " + signParams.sign);
            System.out.println("   签名包含+号: " + signParams.sign.contains("+"));
            
            System.out.println("\n3. 验证签名:");
            var validationResult = tokenUtil.validateSignParams(
                signParams.fileId, 
                signParams.timestamp, 
                signParams.nonce, 
                signParams.sign
            );
            System.out.println("   验证结果: " + validationResult.isValid());
            System.out.println("   文件ID: " + validationResult.getFileId());
            System.out.println("   消息: " + validationResult.getMessage());
            
            System.out.println("\n4. 测试100次生成和验证:");
            boolean allValid = true;
            int plusSignCount = 0;
            for (int i = 0; i < 100; i++) {
                SecurePreviewTokenUtil.SignParams testParams = tokenUtil.generateSignParams((long) i);
                if (testParams.sign.contains("+")) {
                    plusSignCount++;
                }
                var testResult = tokenUtil.validateSignParams(
                    testParams.fileId, 
                    testParams.timestamp, 
                    testParams.nonce, 
                    testParams.sign
                );
                if (!testResult.isValid()) {
                    System.out.println("   验证失败: " + i);
                    allValid = false;
                    break;
                }
            }
            System.out.println("   总共验证: 100次");
            System.out.println("   验证结果: " + (allValid ? "全部成功" : "存在失败"));
            System.out.println("   包含+号的签名数: " + plusSignCount);
            
            System.out.println("\n=== 测试完成 ===");
        } catch (Exception e) {
            System.err.println("测试过程中出现错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}